package org.exhibition.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import org.common.util.Result;
import org.exhibition.domain.entity.UserTicket;
import org.exhibition.domain.pojo.exhibition.UserTicketOrder;
import org.exhibition.mapper.UserTicketMapper;
import org.exhibition.service.ExhibitionService;
import org.exhibition.service.IUserTicketService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.jetbrains.annotations.NotNull;
import org.museum.api.client.IdentityNameClient;
import org.museum.api.util.CommonResponse;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.time.LocalDate;
import java.util.ArrayList;
import java.util.List;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author zafkiel
 * @since 2025-07-16
 */
@Service
public class UserTicketServiceImpl extends ServiceImpl<UserTicketMapper, UserTicket> implements IUserTicketService {
    @Autowired
    private ExhibitionService exhibitionService;
    @Autowired
    private IdentityNameClient identityNameClient;
    @Override
    public Result getUserOrder(Integer id,String exhibitionName) {
        Result result=Result.success();
        // 获取用户订单
        UserTicket userTicket = baseMapper.selectById(id);
        //根据票的id获取票的开始时间
        LocalDate startTime = baseMapper.getTicketStartTimeById(userTicket.getExhiTicketId());
        //根据时间和名字获取详情
        result.put("detail",exhibitionService.getExhibitionDetailByName(exhibitionName,startTime));
        //根据身份证获取姓名
        CommonResponse<String> name = identityNameClient.getIdentityName(userTicket.getIdentity());
        UserTicketOrder userTicketOrder = getUserTicketOrder(name, userTicket);
        List<UserTicketOrder> res=new ArrayList<>();
        res.add(userTicketOrder);
        result.put("userOrder",res);
        return result;
    }

    @Override
    public int payOrder(Long orderId) {
        return baseMapper.update(new UserTicket().setStatus(1),new QueryWrapper<UserTicket>().eq("id", orderId).eq("status", 0));
    }

    @NotNull
    private static UserTicketOrder getUserTicketOrder(CommonResponse<String> name, UserTicket userTicket) {
        UserTicketOrder userTicketOrder = new UserTicketOrder();
        //设置用户姓名
        userTicketOrder.setName(name.getData());
        //设置身份证
        userTicketOrder.setIdentity(userTicket.getIdentity());
        userTicketOrder.setTicketType(userTicket.getTicketType());
        userTicketOrder.setTicketId(userTicket.getExhiTicketId());
        userTicketOrder.setTicketCost(userTicket.getTicketCost());
        userTicketOrder.setStatus(userTicket.getStatus());
        userTicketOrder.setCreateTime(userTicket.getCreateTime());
        userTicketOrder.setId(userTicket.getId());
        return userTicketOrder;
    }
}
